﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TestCryptography
{
    class Program
    {
        //  Call this function to remove the key from memory after use for security.
        [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
        public static extern bool ZeroMemory(ref string Destination, int Length);

        // Function to Generate a 64 bits Key.
        static string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            // Use the Automatically generated key for Encryption. 
            //byte[] x = System.Text.UTF8Encoding.Default.GetBytes(desCrypto.Key);
            
            //ASCIIEncoding.ASCII.GetString(desCrypto.Key);

            return string.Empty;
        }

        //public static string ToHex(byte[] bytes)
        //{
        //    StringBuilder sbBytes = new StringBuilder(bytes.Length * 2);
        //    foreach (byte b in bytes)
        //    {
        //        sbBytes.AppendFormat("{0:X2}", b);
        //    }
        //    return sbBytes.ToString();
        //}

        //public static byte[] HexToByte(String hex)
        //{
        //    int NumberChars = hex.Length;

        //    byte[] bytes = new byte[NumberChars / 2];

        //    for (int i = 0; i < NumberChars; i += 2)
        //        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);

        //    return bytes;
        //}

        public static byte[] HexStringToBytes(string hexString)
        {
            SoapHexBinary shb = SoapHexBinary.Parse(hexString);
            return shb.Value;
        }

        public static string BytesToHexString(byte[] bytes)
        {
            SoapHexBinary shb = new SoapHexBinary(bytes);
            return shb.ToString();
        }

        public static string Encrypt(string plainText)
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            string desKey = "TIcXVSltio0=";
            string desIV = "uGLGRdSnNqY=";
            desCrypto.Key = Convert.FromBase64String(desKey);
            desCrypto.IV = Convert.FromBase64String(desIV);
            System.IO.MemoryStream ms = null;
            using (ms = new MemoryStream())
            using (CryptoStream cs = new CryptoStream(ms, desCrypto.CreateEncryptor(), CryptoStreamMode.Write))
            using (StreamWriter sw = new StreamWriter(cs))
            {
                sw.AutoFlush = true;
                sw.Write(plainText);
            }

            return BytesToHexString(ms.ToArray());
        }



        public static string Decrypt(string hexCipherText)
        {
            byte[] cipherBytes = HexStringToBytes(hexCipherText);

            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            string desKey = "TIcXVSltio0=";
            string desIV = "uGLGRdSnNqY=";
            desCrypto.Key = Convert.FromBase64String(desKey);
            desCrypto.IV = Convert.FromBase64String(desIV);
            System.IO.MemoryStream ms = null;
            using (ms = new MemoryStream(cipherBytes))
            using (CryptoStream cs = new CryptoStream(ms, desCrypto.CreateDecryptor(), CryptoStreamMode.Read))
            using (StreamReader sr = new StreamReader(cs))
            {
                return sr.ReadToEnd();
            }
        }

        static void Main(string[] args)
        {
            string ciphertext = Encrypt("hi zhixian");

            

            Console.WriteLine("Cipher text is {0}.", ciphertext);

            string plainText = Decrypt(ciphertext);

            Console.WriteLine("Plain text is {0}.", plainText);

            Console.ReadLine();
        }

        /*
         * 
         * 
         *             //AesCryptoServiceProvider aesCrypto = (AesCryptoServiceProvider)AesCryptoServiceProvider.Create();
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

            
            //Console.WriteLine("Key [{0}]", Convert.ToBase64String(aesCrypto.Key));
            //Console.WriteLine("IV  [{0}]", Convert.ToBase64String(aesCrypto.IV));

            //Key [TIcXVSltio0=]
            //IV  [uGLGRdSnNqY=]
            string desKey = "TIcXVSltio0=";
            string desIV = "uGLGRdSnNqY=";
            desCrypto.Key = Convert.FromBase64String(desKey);
            desCrypto.IV = Convert.FromBase64String(desIV);

            //Key [rBkTCYo1hj9Scn1SuMJfQlPM6Pb3lnlnOuk9XvhX2Bc=]
            //IV  [xbGkZnAWHzTLxeZjw0HqhQ==]
            //string aesKey = "rBkTCYo1hj9Scn1SuMJfQlPM6Pb3lnlnOuk9XvhX2Bc=";
            //string aesIV = "xbGkZnAWHzTLxeZjw0HqhQ==";

            //aesCrypto.Key = Convert.FromBase64String(aesKey);
            //aesCrypto.IV = Convert.FromBase64String(aesIV);


            System.IO.MemoryStream ms = null;
            using (ms = new MemoryStream())
            using (CryptoStream cs = new CryptoStream(ms, desCrypto.CreateEncryptor(), CryptoStreamMode.Write))
            using (StreamWriter sw = new StreamWriter(cs))
            {
                sw.AutoFlush = true;
                sw.Write("hello world zx");
            }

            //MemoryStream memoryStream = new MemoryStream();
            //CryptoStream cryptoStream = new CryptoStream(memoryStream, desCrypto.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            //StreamWriter writer = new StreamWriter(cryptoStream);
            //writer.Write(originalString);
            //writer.Flush();
            //cryptoStream.FlushFinalBlock();
            //writer.Flush();

            ms.ToArray();

         * */
    }
}

